package com.ilovn.app.test.floatnav;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.ilovn.app.test.floatnav.ObservableScrollView.ScrollViewListener;

public class MainActivity extends ListActivity {
	private FrameLayout float_nav;
	private ObservableScrollView scroll;
	private LinearLayout nav;
	Thread thread = null;
	private ListView listView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		float_nav = (FrameLayout) findViewById(R.id.float_nav);
		scroll = (ObservableScrollView) findViewById(R.id.scroll);
		nav = (LinearLayout) findViewById(R.id.nav);
		
		
		
		

		listView = getListView();
		ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
		for (int i = 0; i < 30; i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("ItemTitle", "This is Title.....");
			map.put("ItemText", "This is text.....");
			mylist.add(map);
		}
		SimpleAdapter mSchedule = new SimpleAdapter(this, // 没什么解释
				mylist,// 数据来源
				R.layout.my_listitem,// ListItem的XML实现

				// 动态数组与ListItem对应的子项
				new String[] { "ItemTitle", "ItemText" },

				// ListItem的XML文件里面的两个TextView ID
				new int[] { R.id.ItemTitle, R.id.ItemText });
		// 添加并且显示
		listView.setAdapter(mSchedule);

		scroll.setScrollViewListener(new ScrollViewListener() {

			@Override
			public void onScrollChanged(ObservableScrollView scrollView, int x,
					int y, int oldx, int oldy) {
				// 获取ScrollView的x,y坐标
				int[] location = new int[2];
				scroll.getLocationInWindow(location);
				System.out.println("sc-" + location[0] + "---" + location[1]);
				// 获取浮动导航的x,y坐标
				int[] loc = new int[2];
				nav.getLocationOnScreen(loc);
				System.out.println(loc[1]);
				// 当浮动组件的y <= ScrollView的y坐标时，把固定的帧布局导航显示出来
				if (loc[1] <= location[1]) {
					// 处理一下把原有导航设置INVISIBLE，这样显示效果会好点
					float_nav.setVisibility(View.VISIBLE);
					nav.setVisibility(View.INVISIBLE);
				} else {
					// 记得还原回来
					float_nav.setVisibility(View.GONE);
					nav.setVisibility(View.VISIBLE);
				}
			}
		});

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}
